<?php
namespace app\index\controller;
use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use app\index\model\Archives_file;
use app\index\model\Arc_extend_info;
use app\index\model\Search_model;
use app\index\model\Storeroom;
use app\index\model\Tag;
use app\index\model\TagArchivesRelation;
use app\index\model\TagGroup;
use think\Controller;
use think\Db;
use think\Request;
use think\Session;

class Search extends Common {
	protected $request;
	protected $ac_model;
	protected $a_model;
	protected $acf_model;
	protected $tag_model;
	protected $tag_group_model;
	protected $tag_archives_relation_model;
	protected $storage_model;
	protected $aei_model;

	public function __construct(Request $request = null) {
		parent::__construct($request);
		$this->request = $request;
		$this->ac_model = new ArchivesCategory();
		$this->a_model = new Archives();
		$this->tag_model = new Tag();
		$this->tag_archives_relation_model = new TagArchivesRelation();
		$this->tag_group_model = new TagGroup();
		$this->storage_model = new Storeroom();
		$this->acf_model = new Archives_file();
		$this->aei_model = new Arc_extend_info();

	}
	// 档案搜索
	public function index() {

		return view('index@Search/index');
	}

	public function doSearch() {

		$rev = new Search_model;
		$res = $rev->getSearch();
		// if ($res != 0) {
		// 	foreach ($res as $key => $value) {
		// 		$data = $this->a_model->getDataByCondition(array('arc_id' => $res[$key]['arc_id']));
		// 		$filePath[$key] = $this->getFilePathUrl($data['category_id'], 2);
		// 		$filePath[$key] = $filePath[$key]['path'] . $data['arc_name'] . '.' . $data['arc_type'];
		// 		$res[$key]['path'] = $filePath[$key];
		// 	}
		// }
		return view('index@Search/result', [
			'res' => $res,
		]);
	}

	//在线阅读
	public function onRead() {
		$params = array();
		$id = $this->request->param('id', 0);
		if (empty($id)) {
			jsonOut(0, '参数错误');
		}
		$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
		// $extend_data = $this->aei_model->where(array('arc_id' => $id))->find();
		$mark = Db::table('cn_archives')
		// ->where('file_new_name', 'like', '%' . $file['token'] . '%')
			->where('arc_id', $id)
			->field('mark')
		// ->order('file_id desc')
			->find();
		if (in_array($mark['mark'], ['2', '3', '4'])) {
			$extend_data = Db::table('cn_arc_extend_info')
				->alias('ei')
				->join('cn_arc_extend e', 'e.id = ei.arc_id')
			// ->where('file_new_name', 'like', '%' . $file['token'] . '%')
				->where('extend_id', $id)
			// ->field('arc_id,value')
			// ->order('file_id desc')
				->select();
		}
		if (empty($data)) {
			jsonOut(0, '文件不存在');
		}
		$res = $this->acf_model->where(array('arc_id' => $id))->find();
		$res1 = $this->fl_model->where(array('file_id' => $res['file_id']))->find();
		$filePath = $res1['file_save_path'];
		// $filePath = $this->getFilePathUrl($data['category_id'], 2);
		// dump($filePath['path']);
		// dump($data['arc_name']);
		//文件夹还有问题  先写死
		// $filePath = $filePath['path'] . $data['arc_name'] . '.' . $data['arc_type'];
		$data['filePath'] = $filePath;
		$data['http'] = 'http://' . $_SERVER['HTTP_HOST'];
		$data['box_path'] = $this->storage_model->getStoragePath($data['box_id']);
		$data['arc_time'] = date('Y-m-d', strtotime($data['arc_time']));
		$params['data'] = $data;
		if (isset($extend_data)) {
			$params['extend_data'] = $extend_data;
		}
		$ac_data = $this->storage_model->where(array('is_delete' => 0))->select();
		$cate_tree = recursionStorage($ac_data, 'sto_id', 0, 'patent_id');
		$tree = createStorageTree($cate_tree, 'sto_id', 'patent_id', 'sto_name');
		if (empty($tree)) {
			jsonOut(0, '文件目录错误');
		}
		$params['tree'] = $tree;
		return view('index@Search/onRead', $params);
	}

	//编辑文档信息
	public function setArc($mark) {
		if ($this->request->instance()->isPost()) {
			$id = $this->request->post('id');
			$arc_title = $this->request->post('arc_title');
			$arc_reference = $this->request->post('arc_reference');
			// $arc_class_num = $this->request->post('arc_class_num');
			// $arc_file_num = $this->request->post('arc_file_num');
			// $arc_total_num = $this->request->post('arc_total_num');
			// $arc_part_num = $this->request->post('arc_part_num');
			// $arc_num = $this->request->post('arc_num');
			$arc_establishment_organization = $this->request->post('arc_establishment_organization');
			$arc_establishment_person = $this->request->post('arc_establishment_person');
			$arc_year = $this->request->post('arc_year');
			$arc_rank = $this->request->post('arc_rank');
			$arc_entities_num = $this->request->post('arc_entities_num');
			$arc_borrowed = $this->request->post('arc_borrowed');
			$arc_page_num = $this->request->post('arc_page_num');
			$box_id = $this->request->post('box_id');
			$arc_storage_position = $this->request->post('arc_storage_position');
			$arc_remarks = $this->request->post('arc_remarks');
			$arc_time = $this->request->post('arc_time');
			$arc_time_limit = $this->request->post('arc_time_limit');
			$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
			$code = Session::get('code');
			Session::delete('code');
			if (empty($data)) {
				jsonOut(0, '档案信息错误');
			}
			//先查询档案盒里面是否已经满了
			$h_data = $this->storage_model->getDataByCondition(array('sto_id' => $box_id));
			if (empty($h_data)) {
				jsonOut(0, '档案盒不能为空');
			}
			$h_sy = $h_data['sto_available_pages'] - $h_data['to_available_pages'];
			if ($arc_page_num > $h_sy) {
				jsonOut(0, '档案盒最大还可以存放' . $h_sy . '页数据');
			}
			if ($mark == '1') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);

			} elseif ($mark == '2') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);
				$extend_data = array(
					'arc_id' => $id,
					'ptime' => $_POST['ptime'],
					'pcha' => $_POST['pcha'],
					'pkey' => $_POST['pkey'],
					'pbrief' => $_POST['pbrief'],
					'pfrom' => $_POST['pfrom'],
					// 'psource' => $_POST['psource'],
					'add_time' => date('Y-m-d H:i:s'),
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);
				$extend = $this->aei_model->updateByCondition(array('arc_id' => $id), $extend_data);

			} elseif ($mark == '3') {
				$save_data = array(
					'arc_title' => $_POST['arc_title'],
					'arc_reference' => $arc_reference,
					// 'arc_class_num'=>$arc_class_num,
					// 'arc_file_num'=>$arc_file_num,
					// 'arc_total_num'=>$arc_total_num,
					// 'arc_part_num'=>$arc_part_num,
					// 'arc_num'=>$arc_num,
					'arc_establishment_organization' => $arc_establishment_organization,
					'arc_establishment_person' => $arc_establishment_person,
					'arc_year' => $arc_year,
					'arc_rank' => $arc_rank,
					'arc_entities_num' => $arc_entities_num,
					'arc_borrowed' => $arc_borrowed,
					'box_id' => $box_id,
					'arc_storage_position' => $arc_storage_position,
					'arc_remarks' => $arc_remarks,
					'arc_time' => $arc_time . " 00:00:00",
					'arc_time_limit' => $arc_time_limit,
					'arc_page_num' => $arc_page_num,
				);

				$extend_data = array(
					'arc_id' => $id,
					'vtime' => $_POST['vtime'],
					'vformat' => $_POST['vformat'],
					'vmin' => $_POST['vmin'],
					'vkey' => $_POST['vkey'],
					'vbrief' => $_POST['vbrief'],
					'vfrom' => $_POST['vfrom'],
					// 'vsource' => $_POST['vsource'],
					'add_time' => date('Y-m-d H:i:s'),
				);
				$save = $this->a_model->updateByCondition(array('arc_id' => $id), $save_data);
				$extend = $this->aei_model->updateByCondition(array('arc_id' => $id), $extend_data);

			}

			if (isset($save) || isset($extend)) {

				//先把之前的档案盒页数开放
				$old_sto_data = $this->storage_model->getDataByCondition(array('sto_id' => $data['box_id']));
				$this->storage_model->updateByCondition(array('sto_id' => $data['box_id']), array('to_available_pages' => $old_sto_data['to_available_pages'] - $arc_page_num));
				//在把对应的档案盒页数减少
				$this->storage_model->updateByCondition(array('sto_id' => $box_id), array('to_available_pages' => $h_data['to_available_pages'] + $arc_page_num));
				jsonOut(1, '文档信息编辑成功');
			} else {
				jsonOut(0, '文档信息编辑失败');
			}
		}
	}

	//收藏文件
	public function shouc($id) {
		// $params = array();
		// $id = $this->request->param('id', 0);
		// if (empty($id)) {
		// 	jsonOut(0, '参数错误');
		// }
		// dump($id);
		$data = $this->a_model->getDataByCondition(array('arc_id' => $id));
		// dump($data);die;
		if (empty($data)) {
			jsonOut(0, '文件不存在');
		}
		$params['data'] = $data;
		if ($this->request->instance()->isPost()) {
			//先创建 收藏表数据
			$collect = new Collect();
			$collect_arc = new CollectArchivesRelation();
			//先查询用户是否已经收藏过
			$f_data = $collect_arc->getDataByCondition(array('arc_id' => $id, 'create_user_id' => $this->admin_session['id']));
			if ($f_data) {
				jsonOut(0, '您已经收藏过了');
			}
			$c_data = array(
				'create_user_id' => $this->admin_session['id'],
				'create_time' => date('Y-m-d H:i:s'),
				'update_user_id' => $this->admin_session['id'],
				'update_time' => date('Y-m-d H:i:s'),
			);
			if ($collect->insert($c_data)) {
				//插入收藏关联表
				$ca_data = array(
					'arc_id' => $id,
					'col_id' => $collect->getLastInsID(),
					'create_user_id' => $this->admin_session['id'],
					'create_time' => date('Y-m-d H:i:s'),
					'update_user_id' => $this->admin_session['id'],
					'update_time' => date('Y-m-d H:i:s'),
				);
				if ($collect_arc->insert($ca_data)) {
					jsonOut(1, '收藏成功');
				} else {
					jsonOut(0, '收藏失败');
				}
			}
		}
		dump($params);die;
		// return view('index@Arc/shouc', $params);
	}

}
